/*
 * Copyright (C) 2011 Samsung Electronics. All rights reserved.
 * Copyright (C) 2012 Sony Computer Entertainment Inc.
 * Copyright (C) 2014 Sony Computer Entertainment Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY MOTOROLA INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MOTOROLA INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "WebProcess.h"

#include "WebProcessCreationParameters.h"
#include <WebCore/NotImplemented.h>
#include <WebCore/RunLoop.h>
#include <manx/NetworkProfile.h>
#include <manx/NetworkUtility.h>
#include <wtf/text/CString.h>

namespace WebKit {

void WebProcess::platformSetCacheModel(CacheModel)
{
    notImplemented();
}

void WebProcess::platformClearResourceCaches(ResourceCachesToClear)
{
    notImplemented();
}

void WebProcess::platformInitializeWebProcess(const WebProcessCreationParameters& parameters, CoreIPC::MessageDecoder&)
{
    CString userIDString = String::number(parameters.userID).ascii();
    setenv("WEBKIT_USER_ID", userIDString.data(), 1);
    setenv("WEBKIT_NET_SKIP_CLEAR_SESSCOOKIE", "1", 1);

#if OS(ORBIS)
    const String cookieJarFile = parameters.cookieStorageDirectory + "/" COOKIE_JAR_FILENAME_DEFAULT;
    Manx::NetworkProfile::set(Manx::NetPropsCookieFilename, cookieJarFile.utf8().data());

    const char* secureWebProcess = getenv("MANX_SECURE_WEB_PROCESS");
    if (secureWebProcess && !strcmp(secureWebProcess, "1"))
        Manx::NetworkProfile::set(Manx::NetPropsMaximumHTTPConnectionCountPerHost, 8);
#endif
    WebNetworkAccessManager::setup();
}

void WebProcess::platformTerminate()
{
    WebNetworkAccessManager::cleanup();
}

void WebProcess::setPrivilegedNetworkBandwidth(uint32_t bandwidthMode)
{
    Manx::NetworkUtility::setPrivilegedNetworkBandwidth(static_cast<Manx::NetworkUtility::BandwidthMode>(bandwidthMode));
}

void WebProcess::setIFilterHTTPProxy(bool enable, String server, int port, String userId, String password)
{
    Manx::NetworkProfile::setIFilterHTTPProxy(enable,
        server.latin1().data(), 
        server.length(),
        port,
        userId.latin1().data(), 
        userId.length(),
        password.latin1().data(), 
        password.length());
}

void WebProcess::setWebSecurityFilter(int filterMode)
{
    Manx::NetworkProfile::set(Manx::NetPropWebSecurityFilterMode, static_cast<long>(filterMode));
}

} // namespace WebKit
